<?php

class Admin_Model_DbTable_Users extends Zend_Db_Table {
	protected $_name = 'users';
	
	public function getUser($id) {
		$id = ( int ) $id;
		$row = $this->fetchRow ( 'id = ' . $id );
		if (! $row) {
			throw new Exception ( "Count not find row $id" );
		}
		return $row->toArray ();
	}
	
	public function addUser($username, $password, $role) {
		$data = array ('username' => $username, 'password' => $password, 'role' => $role );
		$this->insert ( $data );
	}
	
	function updateUser($id, $username, $password, $role) {
		$data = array ('username' => $username, 'password' => $password, 'role' => $role );
		$this->update ( $data, 'id = ' . ( int ) $id );
	
	}
	
	function deleteUser($id) {
		$this->delete ( 'id =' . ( int ) $id );
	}
	
	function checkUnique($username) {
		$select = $this->_db->select ()->from ( $this->_name, array ('username' ) )->where ( 'username=?', $username );
		$result = $this->getAdapter ()->fetchOne ( $select );
		if ($result) {
			return true;
		}
		return false;
	}

}
